﻿using MubiSDK.LoggingLib.Core.Enum;
using MubiSDK.LoggingLib.Core.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MubiSDK.LoggingLib.Core.Formatters
{
    /// <summary>
    /// <para>提供默认的日志格式</para>
    /// <para>日志消息格式：[时间] [线程ID] [日志级别] [日志器名称] [源代码文件名:源代码行号] 日志消息主体</para>
    /// </summary>
    public class DefaultLogFormatter : ILogFormatter
    {
        /// <summary>
        /// 将<see cref="LogMessage"/>对象格式化为字符串
        /// </summary>
        /// <param name="message">要格式化的日志消息</param>
        /// <returns>格式化后的日志字符串(不含换行符)</returns>
        public string Format(LogMessage message)
        {
            string timeFormat = message.LogTimeType == LogTimeType.UtcTime ?
                "yyyy-MM-dd HH:mm:ss 'UTC'" : "yyyy-MM-dd HH:mm:ss 'Local'";

            StringBuilder builder = new StringBuilder();
            builder.Append($"[{message.LogTime.ToString(timeFormat)}] ");
            builder.Append($"[{message.ThreadID}] ");
            builder.Append($"[{message.LogLevel}] ");
            builder.Append($"[{message.LoggerName}] ");
            builder.Append($"[{message.MemberName}({message.FileName}:{message.LineNumber})] ");
            builder.Append($"{message.Payload}");

            return builder.ToString();
        }
    }
}
